package util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SugarUtil {
    /**
     * 处理地图展示的数据格式
     * @param list
     * @return
     */

    public static Map getMapData(List list){
        Map<String, Object> map = new HashMap<>();
        map.put("mapData",list);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status",0);
        resMap.put("data",map);
        return  resMap;
    }

    /**
     * 处理横向柱图展示的数据格式
     */
    public static <T> Map  getBarData(List<T> list){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        //从service中获取地区统计数据
        //存categories的
        Map<String, Object> map= new HashMap<>();
        List categoriesList = new ArrayList();
        //存data的
        List dateList = new ArrayList();
        Map seriesDataMap = new HashMap();

        for (T t : list) {
            Class<?> aClass = t.getClass();
            try {
                Method getName = aClass.getMethod("getName");
                if(getName==null){
                    System.err.println("实体对象:"+t+"没有name属性，或者没有该属性的get方法，请检查实体属性");
                }
                Object invoke = getName.invoke(t);
                categoriesList.add(invoke);
                Method getValue = aClass.getMethod("getValue");
                Object val = getValue.invoke(t);
                dateList.add(val);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        seriesDataMap.put("data",dateList);
        seriesDataMap.put("name","车辆数");
        map.put("categories",categoriesList);
        List objects = new ArrayList<>();
        objects.add(seriesDataMap);
        map.put("series",objects);
        resMap.put("data",map);
        return resMap;
    }


    /**
     * 处理3D饼状图展示的数据格式
     * @param list
     * @return
     * @param <T>
     */
    public static <T> Map  getPie3DData(List<T> list){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        Map<String, Object> map= new HashMap<>();
        Map seriesDataMap = new HashMap();
        seriesDataMap.put("data",list);
        List objects = new ArrayList<>();
        objects.add(seriesDataMap);
        map.put("series",objects);
        resMap.put("data",map);
        return resMap;
    }



    /**
     * 处理饼状图展示的数据格式
     * @param list
     * @return
     * @param <T>
     */
    public static <T> Map  getPieData(List<T> list){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data",list);
        return resMap;
    }



    /**
     * 处理轮播表格数据格式
     * @param
     * @return
     * @param <T>
     */
    public static <T> String getTablePlayData(List<String> headers, List<T> data) {
        try {
            // 参数校验
            if (headers == null || data == null) {
                return "{\"status\":1,\"message\":\"参数不能为null\"}";
            }

            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> result = new HashMap<>();
            result.put("status", 0);

            // 处理表头
            List<Map<String, String>> columns = new ArrayList<>();
            for (int i = 0; i < headers.size(); i++) {
                Map<String, String> column = new HashMap<>();
                column.put("name", headers.get(i));
                column.put("id", "col" + (i + 1));
                columns.add(column);
            }

            // 处理数据行
            List<Map<String, String>> rows = new ArrayList<>();
            for (T item : data) {
                if (item == null) continue;

                Map<String, String> row = new HashMap<>();
                Field[] fields = item.getClass().getDeclaredFields();

                for (int i = 0; i < fields.length && i < headers.size(); i++) {
                    try {
                        fields[i].setAccessible(true);
                        Object value = fields[i].get(item);
                        row.put("col" + (i + 1), value != null ? value.toString() : "");
                    } catch (Exception e) {
                        row.put("col" + (i + 1), "");
                    }
                }
                rows.add(row);
            }

            // 构建结果
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("columns", columns);
            dataMap.put("rows", rows);
            result.put("data", dataMap);

            return mapper.writeValueAsString(result);
        } catch (Exception e) {
            return "{\"status\":1,\"message\":\"生成JSON时出错: " + e.getMessage() + "\"}";
        }
    }



    /**
     * 数字翻码牌
     * @return
     * @param <T>
     */
    public static <T> Map  getFlipNumberData(T val){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data",new BigDecimal(val+""));
        return resMap;
    }


}
